clear all
set more off
capture log close
set matsize 11000 

*---- Set Directory ----*
global rootDir = "/Users/AliceZhang/Dropbox/Research_Columbia/Renewables Voting (Urpelainen Zhang)/JOP/UZ_JOP2021_Replication"
global dataDir = "Data"
global resultDir = "Results"
global logDir = "Analysis/logSTATA"
global graphDir = "Results/Figures"

*---- Log File Path ----*
cd "$rootDir/$logDir"
log using 010_analysis_CBP, replace


*******************************************************************************
/*				          URPELAINEN & ZHANG 2021	        				 */
*******************************************************************************

/* 

File Name:    	009_analysis_CCES.do

By:				Alice Tianbo Zhang (alice.tianbo.zhang@gmail.com)

Last Edited:  	10/16/2021

Purpose:		

Data Used:      votes_wind_panel_CBP.dta

Program Used:   - reghdfe -

*/



*******************************************************************************
/*               	IV REGRESSION - INDUSTRY EMPLOYMENT PANEL			     */
*******************************************************************************

cd "$rootDir/$dataDir/Final"
use votes_wind_panel_CBP.dta, clear

* Rename employment variables 
drop emp_237130 emp_221119 emp_926130 emp_333611 emp_wind
rename emp_fm_237130 emp_237130
rename emp_fm_221119 emp_221119
rename emp_fm_926130 emp_926130
rename emp_fm_333611 emp_333611
rename emp_fm_wind emp_wind

* Rescale payroll variables ($1000)
foreach var of varlist qp1_237130-ap_wind {
	replace `var' = `var' / 1000
}

** Create instrument and fixed effects
gen t = year - 2003
gen inter = t * mean_wp

egen stateyear_fixed = group(state year)
egen district_fixed = group(state district)

gen cum_lncapacity_turbine = log(cum_capacity_turbine + 1)
gen cum_lncount_turbine = log(cum_count_turbine +1 )


*---------------- Employment, Payroll, Establishment by Industry -------------*
// Note: Exclude NAICS 926130 (Wind generated electrical power regulation) due to all zeros

local outcome emp_237130 emp_221119 emp_333611 emp_wind ///
				qp1_237130 qp1_221119 qp1_333611 qp1_wind ///
				ap_237130 ap_221119 ap_333611 ap_wind ///
				est_237130 est_221119 est_333611 est_wind // outcome variable
local endogenous cum_capacity_turbine cum_count_turbine cum_lncapacity_turbine cum_lncount_turbine  // endogenous variable
local instrument inter // instrument
local admin1_trend stateyear_fixed // geography * time trend
local admin2 district_fixed // panel unit (cluster variable)

/* List of variables:
		
  1. outcome 		- dependent variable
  2. endogenous 	- independent variable (endogeous) to be instrumented
  3. instrument 	- instrument
  4. admin1_trend 	- admin1-specific time trend (i.e. state by year fixed effect) 
  5. admin2 		- panel unit and cluster variable (i.e congressional district fixed effect)

*/

foreach y in `outcome' {
	// Create outcome variable label for storing estimates
	tokenize "`y'", parse("_")
	local ind = substr("`3'",1,2)
	local y_name "`1'`ind'"
	di "`y_name'"
	
	foreach x in `endogenous' {
			// Create endogenous variable label for storing estimates
			tokenize "`x'", parse("_")
			local x_name "`3'"
			di "`x_name'"
			
			// Run IV regression
			reghdfe `y' (`x' = `instrument'), absorb(`admin1_trend' `admin2') vce(cluster `admin2') old
			
			* reghdfe `y' (`x' = `instrument'), absorb(`admin1_trend' `admin2') ffirst stages(first ols reduced) vce(cluster `admin2')
			
			// Store IV, first stage, OLS, reduced form estimates
			estimates store `y_name'_`x_name'_iv
		}
}

*--------------------- Export LaTeX regression tables -----------------------*
cd "$rootDir/$resultDir/Tables"

* March Employment
esttab emp23_lncapacity_iv emp23_lncount_iv emp22_lncapacity_iv emp22_lncount_iv ///
		emp33_lncapacity_iv emp33_lncount_iv ///
		using Table7.tex, booktabs replace ///
		refcat(cum_lncapacity_turbine "\emph{Panel A: March Employment}", nolabel) ///
		b(%9.3f) se noconstant nonotes ///
		stats(N N_clust r2, labels("Observations" "Districts" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(cum_lncapacity_turbine "log(Cumulative capacity+1)" cum_lncount_turbine "log(Cumulative count+1)") varwidth(27) modelwidth(13) ///
		mtitles("Model" "Model" "Model" "Model" "Model" "Model") ///
		mgroups("Construction" "Power Generation" "Manufacturing", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)
		
* Annual Payroll ($1000)
esttab ap23_lncapacity_iv ap23_lncount_iv ap22_lncapacity_iv ap22_lncount_iv ///
		ap33_lncapacity_iv ap33_lncount_iv ///
		using Table7.tex, booktabs append ///
		refcat(cum_lncapacity_turbine "\emph{Panel B: Annual Payroll (thousands)}", nolabel) ///
		b(%9.3f) se noconstant nonotes ///
		stats(N N_clust r2, labels("Observations" "Districts" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(cum_lncapacity_turbine "log(Cumulative capacity+1)" cum_lncount_turbine "log(Cumulative count+1)") varwidth(27) modelwidth(13) ///
		mtitles("Model" "Model" "Model" "Model" "Model" "Model") ///
		mgroups("Construction" "Power Generation" "Manufacturing", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)
		
* Number of Establishments
esttab est23_lncapacity_iv est23_lncount_iv est22_lncapacity_iv est22_lncount_iv ///
		est33_lncapacity_iv est33_lncount_iv ///
		using Table7.tex, booktabs append ///
		refcat(cum_lncapacity_turbine "\emph{Panel C: No. Establishments}", nolabel) ///
		b(%9.3f) se noconstant legend star(* 0.10 ** 0.05 *** 0.01) ///
		stats(N N_clust r2, labels("Observations" "Districts" "\(R^{2}\)") fmt(0 0 2)) ///
		varlabels(cum_lncapacity_turbine "log(Cumulative capacity+1)" cum_lncount_turbine "log(Cumulative count+1)") varwidth(27) modelwidth(13) ///
		mtitles("Model" "Model" "Model" "Model" "Model" "Model") ///
		mgroups("Construction" "Power Generation" "Manufacturing", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)
	
** Close log file
log close
cd "$rootDir/$logDir"
translate 010_analysis_CBP.smcl 010_analysis_CBP.log, replace

